﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq.Expressions;
using Newtonsoft.Json.Linq;
using WebGrease.Css.Extensions;

using CanDoo;
using CanDoo.Core;
using CanDoo.Data;
using CanDoo.Web;
using CanDoo.FineUI;
using CanDoo.Extensions;
using CanDoo.Oil.Core.Models;
using CanDoo.Oil.Core.Contracts;
using CanDoo.Admin.Core.Contracts;
using CanDoo.Core.Data;
using FineUIPro;
using CanDoo.Core.Context;

namespace CanDoo.Oil.WebApp.cusCustomer
{
    public partial class Default : ListPageBase<ICustomerContract, Customer>
    {
        public IOperator Operator { get; set; }

        public ICustomerLevelContract CustomerLevelContract { get; set; }
        public IProductContract ProductContract { get; set; }
        public ICustomerContract CustomerContract { get; set; }
        public IUserContract UserContract { get; set; }
        public int CardType = Web.Request.QueryString("cardType").ToInt();

        public override bool IsMaxMode
        {
            get
            {
                return true;
            }
        }

        public bool EnableActive
        {
            get
            {
                return ViewState["EnableActive"].ToBool();
            }
            set
            {
                ViewState["EnableActive"] = value;
            }
        }
        public bool EnableBurn
        {
            get
            {
                return ViewState["EnableBurn"].ToBool();
            }
            set
            {
                ViewState["EnableBurn"] = value;
            }
        }
        

        protected override void PagePowerCheck()
        {
            base.PagePowerCheck();
            EnableActive = PermissionManager.HasPermission(this.PageCode + "Active");
            EnableBurn = PermissionManager.HasPermission(this.PageCode + "Burn");
        }

        protected override void SetControlInPage()
        {
            base.SetControlInPage();
            ShowLimitError.CheckPowerWithButton(EnableActive, this.btnActive);
            ShowLimitError.CheckPowerWithButton(EnableBurn, this.btnBurn);
        }

        #region 方法
        protected override void InitGrid()
        {
            Selector = p => new
            {
                p.CardNo,
                p.Sex,
                p.MobilePhone,
                p.ImageUrl,
                p.CardBank,
                p.CardName,
                p.Province,
                p.City,
                p.Area,
                p.Street,
                p.Address,
                p.CustomerLevel,
                p.CustomerLevelId,
                p.CardType,
                p.LimitProduct,
                p.LimitProductId,
                p.Balance,
                p.TotalAmount,
                p.OnLinePayMoney,
                p.CostedMoney,
                p.CashBackAmount,
                p.Integral,
                p.ServiceCenter,
                p.ServiceCenterId,
                p.Parent,
                p.ParentId,
                p.IsOnline,
                p.IsLocked,
                p.StateUpdateTime,
                p.Status,
                p.Activate,
                p.ActivateId,
                p.ActivateTime,
                p.Remark,
                p.Id,
                p.Name,
                p.IsBurn,
                p.Denomination
            };
            base.InitGrid();
        }

        protected override void Search()
        {
            var key = SearchKey.GetValue();
            var customerlevelId = SearchCustomerLevel.GetValue().ToLong();
            if(string.IsNullOrEmpty(key) == false)
            {
                Filter = Filter.And(p => p.Name.Contains(key) || p.CardNo.Contains(key));
            }
            if(CardType!=0)
                Filter = Filter.And(p => p.CardType == CardType);

            var isDeleted = ManageRecycle.Checked;
            Filter = Filter.And(p => p.IsDeleted == isDeleted);

            var cardNoStart = ed_CardNoStart.GetValue();
            var cardNoEnd = ed_CardNoEnd.GetValue();
            if (!string.IsNullOrEmpty(cardNoStart))
            {
                Filter = Filter.And(p => p.CardNo.CompareTo(cardNoStart) >= 0);
            }
            if (!string.IsNullOrEmpty(cardNoEnd))
            {
                Filter = Filter.And(p => p.CardNo.CompareTo(cardNoEnd) <= 0);
            }

            var active = ed_Active.GetValue().ToInt();
            if (active > 0)
            {
                if(active == 1)
                    Filter = Filter.And(p => p.Status == true);
                else
                    Filter = Filter.And(p => p.Status == false);
            }
        }
        #endregion

        protected override void RegNewBtn()
        {
            _btnAdd.OnClientClick = FineUIShowHelper.GetAddUrl(_WindowEdit,"", PageTitle, IsMaxMode,"CardType="+CardType);
        }

        #region 事件
        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FineUIListHelper.BindDDL(CustomerLevelContract.EntityName, this.CustomerLevelContract.GetSelectList(), 0L, SearchCustomerLevel);
                InitGrid();
            }
        }
        #endregion

        #endregion 事件

        protected void btnActive_Click(object sender, EventArgs e)
        {
            var ids = this.GridList.GetSelectedDataKeyIDs();
            var or = DB.Save(()=> {
                foreach (var id in ids)
                {
                    var entity = CustomerContract.GetById(id);
                    entity.Status = !entity.Status;
                    entity.ActivateId = Operator.UserId;
                    entity.ActivateTime = DateTime.Now;
                    CustomerContract.UpdateEntity(entity);
                }
                return OperationResult.Success;
            });
            FineUIPro.Alert.ShowInTop(or.Message);
            this.InitGrid();
        }

        protected void btnBurn_Click(object sender, EventArgs e)
        {
            var ids = this.GridList.GetSelectedDataKeyIDs();
            var or = DB.Save(() => {
                foreach (var id in ids)
                {
                    var entity = CustomerContract.GetById(id);
                    entity.IsBurn = !entity.IsBurn;
                    entity.ActivateId = Operator.UserId;
                    entity.ActivateTime = DateTime.Now;
                    CustomerContract.UpdateEntity(entity);
                }
                return OperationResult.Success;
            });
            FineUIPro.Alert.ShowInTop(or.Message);
            this.InitGrid();
        }

        public string ShowBalance(object balance, object cardtype)
        {
            if (cardtype.ToInt() == 2)
                return (balance.ToDouble() * 0.001) + "KG";
            else
                return balance.ToDouble() + "元";
        }
    }
}
